package com.imooc.disruptor.server;

import com.imooc.disruptor.disruptor.MessageConsumer;
import com.imooc.disruptor.entity.TranslatorData;
import com.imooc.disruptor.entity.TranslatorDataWrapper;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yqc 2018/10/17
 */
@Slf4j
public class MessageConsumerImplForServer extends MessageConsumer {

    public MessageConsumerImplForServer(String consumerId) {
        super(consumerId);
    }

    @Override
    public void onEvent(TranslatorDataWrapper event) throws Exception {
        //1 业务处理逻辑
        TranslatorData request = event.getData();
        ChannelHandlerContext ctx = event.getCtx();
        log.info("Server端: id = {}, name = {}, message = {}",
                request.getId(), request.getName(), request.getMessage());

        //2 回送响应信息
        TranslatorData response = new TranslatorData();
        response.setId("resp: " + request.getId());
        response.setName("resp: " + request.getName());
        response.setMessage("resp: " + request.getMessage());
        ctx.writeAndFlush(response);
    }
}
